var loadingLayer;
var backLayer;
var ballList = [];
var newballList = [];
var startballList = [];
var renewballList = [];
var renewMuballPosition;
var muball;
var userLeft = "A001";
var userRight = "B001";
var btPower;
var isPowering = false;
var isMoving = false;
var powerNumber = 0;
var powerChang = 1;
var mousePoint = new LPoint(-1, -1) ;
var middlePoint = new LPoint(410, 130);
var leftPoint = new LPoint(310, 130);
var rightPoint = new LPoint(510, 130);
var EPSILON = 0.1;
var ballRadius = 5;
var preballListPosition = [];
var premuballPosition;
var frameE = 0;
var btScoreL;
var numScoreL = 0;
var btScoreR;
var numScoreR = 0;
var isFoul = false;
var foul;
var triangle;
var batNumText;
var isLeftBat = true;
var isEnd = false;
var halfRound = 1;
var imglist = {};
var imgData = new Array(
		{name:"b1",path:"./images/button1.png"},
		{name:"b2",path:"./images/button2.png"},
		{name:"power",path:"./images/power.png"},
		{name:"floor",path:"./images/floor.png"},
		{name:"ball",path:"./images/ball.png"},
		{name:"ball2",path:"./images/ball2.png"},
		{name : "num_0",path : "./images/num_0.png"}, 
		{name : "num_1",path : "./images/num_1.png"}, 
		{name : "num_2",path : "./images/num_2.png"}, 
		{name : "num_3",path : "./images/num_3.png"}, 
		{name : "num_4",path : "./images/num_4.png"}, 
		{name : "num_5",path : "./images/num_5.png"}, 
		{name : "num_6",path : "./images/num_6.png"}, 
		{name : "num_7",path : "./images/num_7.png"}, 
		{name : "num_8",path : "./images/num_8.png"}, 
		{name : "num_9",path : "./images/num_9.png"}, 
		{name : "colon",path : "./images/colon.png"}, 
		{name:"m",path:"./images/m.png"},
		{name:"wall1",path:"./images/wall1.png"},
		{name:"wall2",path:"./images/wall2.png"},
		{name:"bl1",path:"./images/buttonl1.png"},
		{name:"bl2",path:"./images/buttonl2.png"},
		{name:"bl3",path:"./images/buttonl3.png"},
		{name:"bl4",path:"./images/buttonl4.png"},
		{name:"powerboard",path:"./images/powerboard.png"},
		{name:"triangle",path:"./images/triangle.png"},
		{name:"userimage",path:"./images/user.png"},
		{name:"desk",path:"./images/desk.png"}
		);

LInit(20,"mylegend",820,430,main);

window.onorientationchange=window.onresize=function(){
	LSystem.screen(LStage.FULL_SCREEN);
}
function main(){
	LGlobal.stageScale = LStageScaleMode.SHOW_ALL;
	LSystem.screen(LStage.FULL_SCREEN); 
	LGlobal.setDebug(true);	
	
	backLayer = new LSprite();	
	addChild(backLayer);	
	
	loadingLayer = new LoadingSample3();
	backLayer.addChild(loadingLayer);	
	LLoadManage.load(
		imgData,
		function(progress){
			loadingLayer.setProgress(progress);
		},
		function(result){
			imglist = result;
			backLayer.removeChild(loadingLayer);
			loadingLayer = null;
			gameInit();
		}
	);
}

var wallLayer,guideLineLayer;

function gameInit(event){
	LGlobal.box2d = new LBox2d([0,0],false);
	
	//用户
	var user = new LTextField();
	user.text = userLeft;
	user.size = 10;
	user.x = 2;
	user.y = 80;
	backLayer.addChild(user);
	user = new LTextField();
	user.text = userRight;
	user.size = 10;
	user.x = 780;
	user.y = 80;
	backLayer.addChild(user);
	//击球次数
	batNumText = new LTextField();
	batNumText.text = "3杆";
	batNumText.size = 25;
	batNumText.font = "黑体";
	batNumText.x = 2;
	batNumText.y = 110;
	batNumText.stroke = true;
	batNumText.lineWidth = 2;
	batNumText.lineColor = "#FF0000";
	backLayer.addChild(batNumText);
	//地板
	var btdata = new LBitmapData(imglist["floor"],0,0);  
	var bt = new LBitmap(btdata);
	bt.x = 80;
	bt.y = 30;
	backLayer.addChild(bt);
	//用户头像
	btdata = new LBitmapData(imglist["userimage"],0,0);  
	bt = new LBitmap(btdata);
	bt.x = 2;
	bt.y = 50;
	backLayer.addChild(bt);
	btdata = new LBitmapData(imglist["userimage"],0,0);  
	bt = new LBitmap(btdata);
	bt.x = 780;
	bt.y = 50;
	backLayer.addChild(bt);
	//控制台
	btdata = new LBitmapData(imglist["desk"],0,0);  
	bt = new LBitmap(btdata);
	bt.x = 60;
	bt.y = 353;
	backLayer.addChild(bt);
	//拥有击球权的三角标识
	btdata = new LBitmapData(imglist["triangle"],0,0);  
	triangle = new LBitmap(btdata);
	triangle.x = 2;
	triangle.y = 12;
	backLayer.addChild(triangle);
	LTweenLite.to(triangle,1,{alpha:0,loop:true}).to(triangle,1,{alpha:1});
	//墙
	wallLayer = new LSprite();
	wallLayer.x = 410;
	wallLayer.y = 15;
	backLayer.addChild(wallLayer);
	wallLayer.addBodyPolygon(660,30,0,1,0,1);
	btdata = new LBitmapData(imglist["wall1"],0,0);  
	bt = new LBitmap(btdata);
	wallLayer.addChild(bt);
	wallLayer = new LSprite();
	wallLayer.x = 65;
	wallLayer.y = 105;
	backLayer.addChild(wallLayer);
	wallLayer.addBodyPolygon(30,210,0,1,0,1);
	btdata = new LBitmapData(imglist["wall2"],0,0);  
	bt = new LBitmap(btdata);
	wallLayer.addChild(bt);
	wallLayer = new LSprite();
	wallLayer.x = 755;
	wallLayer.y = 105;
	backLayer.addChild(wallLayer);
	wallLayer.addBodyPolygon(30,210,0,1,0,1);
	btdata = new LBitmapData(imglist["wall2"],0,0);  
	bt = new LBitmap(btdata);
	wallLayer.addChild(bt);
	//比分
	btdata = new LBitmapData(imglist["num_0"],0,0);  
	btScoreL = new LBitmap(btdata);
	btScoreL.x = middlePoint.x - 60;
	btScoreL.y = 2;
	btScoreL.scaleX = 1.1;
	btScoreL.scaleY = 1.1;
	backLayer.addChild(btScoreL);
	btdata = new LBitmapData(imglist["colon"],0,0);  
	bt = new LBitmap(btdata);
	bt.x = middlePoint.x - 5;
	bt.y = 2;
	bt.scaleX = 1.1;
	bt.scaleY = 1.1;
	backLayer.addChild(bt);
	btdata = new LBitmapData(imglist["num_0"],0,0);  
	btScoreR = new LBitmap(btdata);
	btScoreR.x = middlePoint.x + 40;
	btScoreR.y = 2;
	btScoreR.scaleX = 1.1;
	btScoreR.scaleY = 1.1;
	backLayer.addChild(btScoreR);
	//球
	ballCreate(middlePoint.x, middlePoint.y);
	ballCreate(middlePoint.x-10, middlePoint.y);
	ballCreate(middlePoint.x+10, middlePoint.y);
	ballCreate(middlePoint.x-5, middlePoint.y+8.76);
	ballCreate(middlePoint.x+5, middlePoint.y+8.76);
	ballCreate(middlePoint.x-5, middlePoint.y-8.76);
	ballCreate(middlePoint.x+5, middlePoint.y-8.76);
	ballCreate(middlePoint.x, middlePoint.y+17.52);
	ballCreate(middlePoint.x, middlePoint.y-17.52);
	if (isLeftBat) muballCreate(rightPoint.x, middlePoint.y);
	else muballCreate(leftPoint.x, middlePoint.y);
	//击球方向
	backLayer.addEventListener(LMouseEvent.MOUSE_UP,onDrawGuideLine);	
	guideLineLayer = new LSprite();
	backLayer.addChild(guideLineLayer);
	//击球按钮
	var bitmapDataUp = new LBitmapData(imglist["b1"],0,0);
	var bitmapUp = new LBitmap(bitmapDataUp);
	bitmapUp.scaleX = 0.4;
	bitmapUp.scaleY = 0.4;
	var bitmapDataOver = new LBitmapData(imglist["b2"],0,0);
	var bitmapOver = new LBitmap(bitmapDataOver);
	bitmapOver.scaleX = 0.4;
	bitmapOver.scaleY = 0.4;
	var button = new LButton(bitmapUp,bitmapOver,bitmapOver);
	button.x = 650;
	button.y = 354;
	button.staticMode = true;
	backLayer.addChild(button);
	button.addEventListener(LMouseEvent.MOUSE_UP,onKeyup);
	button.addEventListener(LMouseEvent.MOUSE_DOWN,onKeydown);
	button.addEventListener(LMouseEvent.MOUSE_OVER,onKeyover);
	LMouseEventContainer.set(LMouseEvent.MOUSE_DOWN,true);
	LMouseEventContainer.set(LMouseEvent.MOUSE_UP,true);
	//微调按钮
	bitmapDataUp = new LBitmapData(imglist["bl1"],0,0);
	bitmapUp = new LBitmap(bitmapDataUp);
	bitmapDataOver = new LBitmapData(imglist["bl2"],0,0);
	bitmapOver = new LBitmap(bitmapDataOver);
	button = new LButton(bitmapUp,bitmapOver,bitmapOver);
	button.x = 380;
	button.y = 362;
	button.staticMode = true;
	backLayer.addChild(button);
	button.addEventListener(LMouseEvent.MOUSE_DOWN,onLKeydown2);
	
	bitmapDataUp = new LBitmapData(imglist["bl1"],0,0);
	bitmapUp = new LBitmap(bitmapDataUp);
	bitmapDataOver = new LBitmapData(imglist["bl2"],0,0);
	bitmapOver = new LBitmap(bitmapDataOver);
	button = new LButton(bitmapUp,bitmapOver,bitmapOver);
	button.x = 370;
	button.y = 390;
	button.staticMode = true;
	button.setRotate(180);
	backLayer.addChild(button);
	button.addEventListener(LMouseEvent.MOUSE_DOWN,onRKeydown2);
	
	bitmapDataUp = new LBitmapData(imglist["bl3"],0,0);
	bitmapUp = new LBitmap(bitmapDataUp);
	bitmapDataOver = new LBitmapData(imglist["bl4"],0,0);
	bitmapOver = new LBitmap(bitmapDataOver);
	button = new LButton(bitmapUp,bitmapOver,bitmapOver);
	button.x = 180;
	button.y = 362;
	button.staticMode = true;
	backLayer.addChild(button);
	button.addEventListener(LMouseEvent.MOUSE_DOWN,onRKeydown);
	
	bitmapDataUp = new LBitmapData(imglist["bl3"],0,0);
	bitmapUp = new LBitmap(bitmapDataUp);
	bitmapDataOver = new LBitmapData(imglist["bl4"],0,0);
	bitmapOver = new LBitmap(bitmapDataOver);
	button = new LButton(bitmapUp,bitmapOver,bitmapOver);
	button.x = 170;
	button.y = 390;
	button.staticMode = true;
	button.setRotate(180);
	backLayer.addChild(button);
	button.addEventListener(LMouseEvent.MOUSE_DOWN,onLKeydown);
	//力度显示
	btdata = new LBitmapData(imglist["powerboard"],0,0);  
    btPower = new LBitmap(btdata);
	btPower.x = 498;
	btPower.y = 362;
	backLayer.addChild(btPower);
	
	btdata = new LBitmapData(imglist["power"],0,0);  
    btPower = new LBitmap(btdata);
	btPower.x = 500;
	btPower.y = 364;
	backLayer.addChild(btPower);
	var maskObj = new LSprite();
    maskObj.graphics.drawRect(0, "#ff0000", [500, 364, powerNumber, 25]);
    btPower.mask = maskObj;
	//碰撞安全球
	LGlobal.box2d.setEvent(LEvent.END_CONTACT,beginContact);
	//帧
	backLayer.addEventListener(LEvent.ENTER_FRAME, onFrame);
	if (isLeftBat) showInfo("比赛开始，由"+userLeft+"开始击球！");
	else showInfo("比赛开始，由"+userRight+"开始击球！");
}
//判断是否在安全区
function isInsafe(ball){
	if (ball.x + ballRadius> 110 && ball.x + ballRadius < 210 && ball.y + ballRadius < 130){
		return true;
	}
	if (ball.x + ballRadius > 610 && ball.x + ballRadius < 710 && ball.y + ballRadius < 130){
		return true;
	}
	return false;
}
//判断是否碰撞了安全区的球
function beginContact(contact){
	if (!isFoul){
		if(contact.GetFixtureA().GetBody().GetUserData() == muball || contact.GetFixtureB().GetBody().GetUserData() == muball){
			if (isLeftBat && muball.x < rightPoint.x) return;
			if (!isLeftBat && muball.x > leftPoint.x) return;
			if (contact.GetFixtureA().GetBody().GetUserData().safeball == 1 ){
				var ball = contact.GetFixtureA().GetBody().GetUserData();
				if (isInsafe(ball)){
					isFoul = true;
				}
			}
			else if (contact.GetFixtureB().GetBody().GetUserData().safeball == 1 ){
				var ball = contact.GetFixtureB().GetBody().GetUserData();
				if (isInsafe(ball)){
					isFoul = true;
				}
			}
			if (isFoul){
				foul = new LTextField();
				foul.text = "犯规";
				foul.size = 50;
				foul.alpha = 0;
				foul.font = "黑体";
				foul.color = "#FF8040";
				foul.x = (LStage.width-foul.getWidth())*0.5;
				foul.y = (LStage.height-foul.getHeight())*0.5;
				backLayer.addChild(foul);
				LTweenLite.to(foul,0.5,{
				delay:0,
				alpha:1
			});
			}
		}
	}
};
function onFrame(event){

	if (isPowering){
		var maskObj = btPower.mask;
		powerNumber += powerChang;
		maskObj.graphics.clear();
		maskObj.graphics.drawRect(0, "#ff0000", [500, 364, powerNumber, 25]);
		if (powerNumber >= 99 ) powerChang = -1;
		else if (powerNumber <= 0)powerChang = 1;
	}
	if (isMoving){
		
		if (++frameE % 10 == 0){
			var isStop = true;
			var muballStop = true;
			for (var i=0; i<preballListPosition.length; i++){
				if (Math.abs(preballListPosition[i].x - ballList[i].x) > EPSILON || Math.abs(preballListPosition[i].y - ballList[i].y) > EPSILON){
					preballListPosition[i].x = ballList[i].x;
					preballListPosition[i].y = ballList[i].y;
					isStop = false;
				}
				if (ballList[i].y > 330){
					if (ballList[i].x < 110){
						newballList.push("left");
					}
					else if (ballList[i].x <310){
						if (startballList[i].x < 310){
							newballList.push("left");
						}else{
							newballList.push("mid");
						}
					}else if (ballList[i].x < 510){
						newballList.push("mid");
					}
					else if (ballList[i].x < 710){
						if (startballList[i].x < 510){
							newballList.push("mid");
						}else{
							newballList.push("right");
						}
					}else{
						newballList.push("right");
					}
					backLayer.removeChild(ballList[i]);
					ballList.splice(i, 1);
					preballListPosition.splice(i, 1);
					startballList.splice(i, 1);
					i--;
				}else if (ballList[i].x < 50){
					newballList.push("left");
					backLayer.removeChild(ballList[i]);
					ballList.splice(i, 1);
					preballListPosition.splice(i, 1);
					startballList.splice(i, 1);
					i--;
				}else if (ballList[i].x > 760){
					newballList.push("right");
					backLayer.removeChild(ballList[i]);
					ballList.splice(i, 1);
					preballListPosition.splice(i, 1);
					startballList.splice(i, 1);
					i--;
				}
			}
			
			if (muball != null && (muball.x < 50 || muball.x > 760 || muball.y > 330)){
				if (muball.x < 410){
					newballList.push("muleft");
				}else{
					newballList.push("muright");
				}
				backLayer.removeChild(muball);
				muball = null;
			}
			else if (muball != null && (Math.abs(premuballPosition.x - muball.x) > EPSILON || Math.abs(premuballPosition.y - muball.y) > EPSILON)){
				premuballPosition.x = muball.x;
				premuballPosition.y = muball.y;
				muballStop = false;
				
			}
			
			if (isStop && muballStop){
				if (!isFoul){
					for (var i=0; i<ballList.length; i++){
						if (ballList[i].x + ballRadius < 110){
							newballList.push("left");
							backLayer.removeChild(ballList[i]);
							ballList.splice(i, 1);
							preballListPosition.splice(i, 1);
							startballList.splice(i, 1);
							i--;
						}else if (ballList[i].x + ballRadius > 710){
							newballList.push("right");
							backLayer.removeChild(ballList[i]);
							ballList.splice(i, 1);
							preballListPosition.splice(i, 1);
							startballList.splice(i, 1);
							i--;
						}
					}
					if (muball != null && muball.x + ballRadius < 110){
						newballList.push("muleft");
						backLayer.removeChild(muball);
						muball = null;
					}else if (muball != null && muball.x + ballRadius > 710){
						newballList.push("muright");
						backLayer.removeChild(muball);
						muball = null;
					}
					for (var i=0; i<newballList.length; i++){
						if (newballList[i] == "muright"){
							v = findConfortPoint(new LPoint(710, 330), -1);
							muballCreate(v.x, v.y);
						}
						else if (newballList[i] == "muleft"){
							v = findConfortPoint(new LPoint(110, 330), -1);
							muballCreate(v.x, v.y);
						}
						else if (newballList[i] == "left"){
							v = findConfortPoint(new LPoint(leftPoint.x, leftPoint.y), 1);
								ballCreate(v.x, v.y);
					}
						else if (newballList[i] == "right"){
							v = findConfortPoint(new LPoint(rightPoint.x, rightPoint.y), 1);
							ballCreate(v.x, v.y);
						}
						else if (newballList[i] == "mid"){
							v = findConfortPoint(new LPoint(middlePoint.x, middlePoint.y), 1);
							ballCreate(v.x, v.y);
						}
					}
				}else{//isFoul==true
					isFoul = false;
					backLayer.removeChild(foul);
					foul = null;
					for (var i=0; i<ballList.length; i++){
						backLayer.removeChild(ballList[i]);
					}
					ballList = [];
					for (var i=0; i<renewballList.length; i++){
						ballCreate(renewballList[i].x, renewballList[i].y);
					}
					if (muball != null) backLayer.removeChild(muball);
					muballCreate(renewMuballPosition.x, renewMuballPosition.y);
				}
				//比分和安全球
				var scoreL = 0;
				var scoreR = 0;
				for (var i=0; i<ballList.length; i++){
					var r = ballRadius;
					if (ballList[i].name == "new") r = 0;
					if (ballList[i].x + r <= leftPoint.x) scoreL++;
					else if (ballList[i].x + r >= rightPoint.x) scoreR++;
					if (isInsafe(ballList[i])) {
						if (ballList[i].safeball == 0){
							ballList[i].safeball = 1;
							ballList[i].addChild(new LBitmap(new LBitmapData(imglist["ball2"])));
						}
					}else{
						if (ballList[i].safeball == 1){
							ballList[i].safeball = 0;
							ballList[i].addChild(new LBitmap(new LBitmapData(imglist["ball"])));
						}
					}
				}
				btScoreR = changeScore(btScoreR, scoreR, middlePoint.x + 40, 2);
				btScoreL = changeScore(btScoreL, scoreL, middlePoint.x - 60, 2);
				numScoreL = scoreL;
				numScoreR = scoreR;
				//还原状态
				
				powerNumber = 0;
				btPower.mask.graphics.clear();
				isMoving = false;
				//发球权转换
				var batNum = parseInt(batNumText.text);
				if (batNum <= 0){
					changeBatUser();
					batNumText.text = "3杆";
				}else{
					showInfo("继续击球，剩余"+batNumText.text);
				}
			}
		}
		
	}
	
}

function onDrawGuideLine(event){
	if ((event.offsetX < 80 || event.offsetX > 740) || (event.offsetY < 30 || event.offsetY > 335) || isMoving)
		return;
	var p = muball.getRootCoordinate();
	
	mousePoint.x = event.offsetX;
	mousePoint.y = event.offsetY;
	drawLine();
}
//击球
function onKeyup(event){
	if (isEnd) return;
	isPowering = false;
	if (mousePoint.x == -1) return;
	moveVec = new LStage.box2d.b2Vec2(0,0);
	
	if (powerNumber <= 0){
	powerNumber = 1;
	}
	
	var p = muball.getRootCoordinate();
	p.x += ballRadius;
	p.y += ballRadius;
	var sx = mousePoint.x - p.x;
	var sy = mousePoint.y - p.y;
	
	var tanA = sx / sy;
	var A = Math.atan(tanA);
	var sinA = Math.sin(A);
	var cosA = Math.cos(A);
	moveVec.x = sinA*powerNumber/35;
	moveVec.y = cosA*powerNumber/35;
	if ((sx < 0 && moveVec.x > 0) || (sx > 0 && moveVec.x < 0)) moveVec.x = -moveVec.x;
	if ((sy < 0 && moveVec.y > 0) || (sy > 0 && moveVec.y < 0)) moveVec.y = -moveVec.y;

	startballList = [];
	preballListPosition = [];
	renewballList = [];
	for (var i=0; i<ballList.length; i++){
		var r = ballRadius;
		
		startballList.push(new LPoint(ballList[i].x+r, ballList[i].y+r));
		preballListPosition.push(new LPoint(ballList[i].x+r, ballList[i].y+r));
		renewballList.push(new LPoint(ballList[i].x+r, ballList[i].y+r));
		ballList[i].name = "";
		
	}
	
	premuballPosition = new LPoint(muball.x+ballRadius, muball.y+ballRadius);
	renewMuballPosition = new LPoint(muball.x+ballRadius, muball.y+ballRadius);
	newballList = [];
	muball.box2dBody.ApplyForce(moveVec, p);
	isMoving = true;
	guideLineLayer.graphics.clear();
	mousePoint.x = -1;
	var batNum = parseInt(batNumText.text)-1;
	batNumText.text = batNum+"杆";
}
//微调按钮
function onLKeydown2(event){
	if (mousePoint.x == -1) return;
	mp = changeALittle(0, -1);
	if (mp == null) return;
	drawLine();
}
function onRKeydown2(event){
	if (mousePoint.x == -1) return;
	mp = changeALittle(0, 1);
	if (mp == null) return;
	drawLine();
}
function onLKeydown(event){
	if (mousePoint.x == -1) return;
	mp = changeALittle(0.01, 0);
	if (mp == null) return;
	drawLine();
}
function onRKeydown(event){
	if (mousePoint.x == -1) return;
	mp = changeALittle(-0.01, 0);
	if (mp == null) return;
	drawLine();
}
function onKeydown(event){
	if (mousePoint.x == -1 || isEnd) return;
	isPowering = true;
}

function onKeyover(event){
	isPowering = false;
}

function muballCreate(x, y){
	muball = new LSprite();
	muball.x = x;
	muball.y = y;
	muball.addBodyCircle(5,0,0,1,.1,0,1);
	bt = new LBitmap(new LBitmapData(imglist["m"]));
	muball.addChild(bt);
	muball.box2dBody.SetFixedRotation(true);
	muball.box2dBody.SetLinearDamping(0.3);
	backLayer.addChild(muball);
	return muball;
}
function ballCreate(x, y){
	var ball = new LSprite();
	ball.x = x;
	ball.y = y;
	ballList.push(ball);
	ball.addBodyCircle(5,0,0,1,.1,0,1);
	ball.box2dBody.SetFixedRotation(true);
	ball.box2dBody.SetLinearDamping(0.3);
	bt = new LBitmap(new LBitmapData(imglist["ball"]));
	ball.addChild(bt);
	backLayer.addChild(ball);
	ball.name = "new";
	ball.safeball = 0;
	return ball;
}
//碰撞检测
function hitTest(ball, v){
	br = ball.getRootCoordinate();
	var r = ballRadius;
	if (ball.name == "new"){
		r = 0;
	}
	var sx = br.x + r - v.x;
	var sy = br.y + r - v.y;
	if (sx*sx+sy*sy >= 100) {
		return false;
	}
	return true;
}
function hitTestAll(v){
	for (var i=0; i<ballList.length; i++){
		if (hitTest(ballList[i], v)){
			return true;
		}
	}
	if (muball != null && hitTest(muball, v)){
		return true;
	}
	return false;
}
function findConfortPoint(v, e){
	for (var j=0; j<100; j++){
		if (hitTestAll(v)){
			v.y += e;
		}else{
			break;
		}
	}
	return v;
}
function changeScore(btscore, score, x, y){
	backLayer.removeChild(btscore);
	var btdata = new LBitmapData(imglist["num_"+score],0,0);  
	var btscore = new LBitmap(btdata);
	btscore.x = x;
	btscore.y = y;
	btscore.scaleX = 1.1;
	btscore.scaleY = 1.1;
	backLayer.addChild(btscore);
	return btscore;
}
function changeALittle(angle, length){
	var p = muball.getRootCoordinate();
	p.x += ballRadius;
	p.y += ballRadius;
	var sx = mousePoint.x - p.x;
	var sy = mousePoint.y - p.y;
	var sz = Math.sqrt(sx*sx+sy*sy) + length;
	if (sz <= 10){
		return null;
	}
	var tanA = sx / sy;
	var A = Math.atan(tanA);
	A += angle;
	var sinA = Math.sin(A);
	var cosA = Math.cos(A);
	if (sy > 0){
		sx = sinA*sz;
		sy = cosA*sz;
	}else{
		sx = -sinA*sz;
		sy = -cosA*sz;
	}
	mousePoint.x = p.x + sx;
	mousePoint.y = p.y + sy;
	return mousePoint;
}
function drawLine(){
	guideLineLayer.graphics.clear();
	var startPoint = new LPoint(mousePoint.x, mousePoint.y);
	mp = changeALittle(0, -10);
	while (mp != null){
		guideLineLayer.graphics.drawEllipse(1, "#DDDDDD", [mp.x-1, mp.y-1, 2, 2], true, "#DDDDDD");
		mp = changeALittle(0, -7);
	}
	mousePoint.x = startPoint.x;
	mousePoint.y = startPoint.y;
	guideLineLayer.graphics.drawEllipse(1, "#DDDDDD", [mousePoint.x-5, mousePoint.y-5, 10, 10], true, "#DDDDDD");
}
function changeBatUser(){
	isLeftBat = !isLeftBat;
	if (isLeftBat){
		triangle.x = batNumText.x = 2;
	}else{
		triangle.x = batNumText.x = 780;
	}
	if (++halfRound > 12){
		var winner = "";
		if (isLeftBat){
			if (numScoreR > numScoreL) winner=userRight;
			else winner = userLeft;
		}
		else{
			if (numScoreL > numScoreR) winner=userLeft;
			else winner=userRight;
		}
		showInfo("比赛结束，"+winner+"获得胜利", 100);
		endGame();
		return;
	}
	var name = userLeft;
	if (!isLeftBat) name = userRight;
	if (halfRound%2 == 0) {
		showInfo("交换击球权，由"+name+"开始击球");
	}
	else {
		var round = parseInt(halfRound/2)+1;
		showInfo("第"+round+"回合开始，共6回合\n由"+name+"开始击球");
	}
}
function showInfo(s, displaytime){
	var displaytime = arguments[1] ? arguments[1] : 2;
	info = new LTextField();
	info.text = s;
	info.size = 25;
	info.alpha = 1;
	info.font = "黑体";
	info.color = "#FF8040";
	info.x = (LStage.width-info.getWidth())*0.5;
	info.y = (LStage.height-info.getHeight())*0.5 - 50;
	info.scaleX = 0.1;
	info.scaleY = 0.1;
	info.weight = "bolder";
	info.stroke = true;
	info.lineWidth = 1;
	info.lineColor = "#FFFFFF";
	info.width = 500;
	info.setWordWrap(true, 40);
	backLayer.addChild(info);
	LTweenLite.to(info,0.2,{delay:0,scaleX:1,scaleY:1}).to(info,0.2,{delay:displaytime,alpha:0,onComplete:function(e){
        backLayer.removeChild(e.target);//info
    }});
}
function endGame(){
	backLayer.removeChild(triangle);
	backLayer.removeChild(batNumText);
	isEnd = true;
}